﻿using System;
using System.Collections.Specialized;
using System.Configuration;
using System.Net;
using System.Text;
using System.Web;

namespace JinJiang.BookingEngine.Services
{
    public class MemberService
    {
        private static readonly string s_Url;
        private static readonly string s_SecurityKey;

        static MemberService()
        {
            s_Url = string.IsNullOrEmpty(ConfigurationManager.AppSettings["JinJiangGatewayUrl"]) ? "http://uat.ejinjiang.com:9117/gateway" : ConfigurationManager.AppSettings["JinJiangGatewayUrl"];
            s_SecurityKey = string.IsNullOrEmpty(ConfigurationManager.AppSettings["JinJiangSecurityKey"]) ? "ca95e37c94ce954c9cb2276a751eec6a" : ConfigurationManager.AppSettings["JinJiangSecurityKey"];
        }

        public bool UserLogin(string loginName, string password, out MemberInfo memberInfo)
        {
            if (string.IsNullOrEmpty(loginName))
            {
                throw new ArgumentNullException("loginName");
            }
            if (string.IsNullOrEmpty(password))
            {
                throw new ArgumentNullException("password");
            }
            bool success;
            memberInfo = null;
            try
            {
                var requestData = new NameValueCollection
                                      {
                                          {
                                              "loginName", loginName
                                              },
                                          {
                                              "pswd", (Md5Utils.EncodeByMd5(password) ?? string.Empty).ToUpper()
                                              },
                                          {
                                              "source", "hubs1"
                                              },
                                          {
                                              "userId", "jjeapp"
                                              },
                                          {
                                              "sign", (Md5Utils.EncodeByMd5(string.Format("jjeapp{0}", s_SecurityKey)) ?? string.Empty).ToUpper()
                                              }
                                      };
                using (var webClient = new WebClient())
                {
                    webClient.Encoding = Encoding.UTF8;
                    var responseData = webClient.UploadValues(string.Format("{0}/member/login", s_Url), "POST", requestData);
                    memberInfo = new MemberInfo(Encoding.UTF8.GetString(responseData));
                    success = memberInfo.IsSuccess;
                    if (success)
                    {
                        SaveCookie(loginName, password, memberInfo);
                    }
                }
            }
            catch (Exception)
            {
                success = false;
            }
            return success;
        }

        private void SaveCookie(string loginName, string password, MemberInfo memberInfo)
        {
            HttpContext.Current.Session["lgname"] = loginName;
            HttpContext.Current.Session["lgpassword"] = password;
            HttpContext.Current.Session["member"] = memberInfo;
        }

        public void UserLogout()
        {
            HttpContext.Current.Session["lgname"] = null;
            HttpContext.Current.Session["lgpassword"] = null;
            HttpContext.Current.Session["member"] = null;
        }
    }
}